/*
 *  Copyright (C) 2011 Dan de la Cruz <ddcruzg@gmail.com>
 * 
 *  Viajero Fashion is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Viajero Fashion is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.vfashion.dao.impl.mysql;

import com.vfashion.dao.VentaDAO;
import com.vfashion.dao.factory.DAOFactory;
import com.vfashion.domain.Carrito;
import com.vfashion.domain.Cliente;
import com.vfashion.domain.Producto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author Dan de la Cruz <ddcruzg@gmail.com>
 */
public class MysqlVentaDAO implements VentaDAO {

    private Connection mysql;

    public MysqlVentaDAO() {
        mysql = DAOFactory.getInst().getConexion();
    }

    @Override
    public void realizarVenta(Cliente cliente, Carrito carrito) throws Exception {

        int idcliente = cliente.getIdcliente();
        int idventa = -1;
        try {
            mysql.setAutoCommit(false);
            String consulta = "INSERT INTO venta (cliente_idcliente,fecha,dscto) "
                    + "VALUES (?,NOW(),?)";

            PreparedStatement pstmt = mysql.prepareStatement(consulta, Statement.RETURN_GENERATED_KEYS);
            pstmt.setInt(1, cliente.getIdcliente());
            pstmt.setDouble(2, carrito.getAhorro() );
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                idventa = rs.getInt(1);
                System.out.println("idVenta : " + idventa);
            } else {
                System.out.println("Error : no se genero la venta");
                System.out.println("idVenta : " + idventa);
            }

            consulta = "INSERT INTO det_venta "
                    + "(paquete_idpaquete,venta_idventa,cantidad,importe)"
                    + "VALUES (?,?,?,?)";

            pstmt = mysql.prepareStatement(consulta);
            for (Producto p : carrito.getProductos()) {
                
                pstmt.setInt(1, p.getPaquete().getIdpaquete());
                pstmt.setInt(2, idventa);
                pstmt.setInt(3, p.getCantidad());
                pstmt.setDouble(4, p.getTotal());
                pstmt.executeUpdate();
            }
            mysql.setAutoCommit(true);

        } catch (Exception e) {
            mysql.rollback();
            System.out.println("rollback : "+e.getMessage());
        } finally {
            try {
                System.out.println("venta realizada satisfactoriamente");
                mysql.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
